/<template>
  <div class="content">
    <el-form
      ref="fromData"
      :model="fromData"
      :rules="rules"
      label-position="top"
      label-width="120px"
      class="form"
    >
      <el-form-item
        label="卡券名称"
        prop="coupon_name"
      >
        <el-input
          v-model="fromData.coupon_name"
          placeholder="请输入卡券名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="权益内容"
        prop="gift_num"
      >
        <div class="Equity">
          赠
          <el-input
            v-model="fromData.gift_num"
            oninput="value=value.replace(/[^1-9]/g, '')"
            type="number"
            style="width: 150px;"
            placeholder="请输入赠集数"
          ></el-input>集
        </div>
      </el-form-item>
    </el-form>
    <span class="dialog-footer rowFlex">
      <el-button
        class="btn"
        @click="close"
      >取 消</el-button>
      <el-button
        :loading="loading"
        class="btn"
        type="primary"
        @click="submit"
      >确 定</el-button>
    </span>
  </div>
</template>

<script>
import { couponSave } from '@/api/coupons'
export default {
  props: {
    rowInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      loading: false,
      rules: {
        coupon_name: [{ required: true, message: '请输入卡券名称', trigger: 'blur' }],
        gift_num: [{ required: true, message: '请输入权益内容', trigger: 'blur' }]
      },
      fromData: {
        coupon_name: '',
        gift_num: ''
      }
    }
  },
  mounted() {
    console.log(this.rowInfo)
    if (this.rowInfo.id) {
      this.fromData = {
        coupon_name: this.rowInfo.coupon_name,
        gift_num: this.rowInfo.gift_num
      }
    }
  },
  methods: {
    close() {
      this.$emit('cancel')
    },
    async onCouponSave() {
      try {
        console.log('132')
        const data = {
          ...this.fromData
        }
        if (this.rowInfo.id) {
          data.id = this.rowInfo.id
        }
        const res = await couponSave(data)
        if (res.status_code === 1) {
          this.loading = false
          this.$emit('ok')
        }
      } catch (error) {
        console.log(error)
        this.loading = false
      }
    },
    async submit() {
      this.$refs.fromData.validate((valid) => {
        if (valid) {
          this.loading = true
          this.onCouponSave()
        } else {
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  padding: 20px;
}
.Equity {
  display: flex;
  width: 200px;
  justify-content: space-between;
}
.rowFlex {
  left: 0;
}
</style>